// pages/home/home.js

import {Theme} from "../../models/theme";
import {Banner} from "../../models/banner";
import {Category} from "../../models/category";
import {Activity} from "../../models/activity";
import {SpuPaging} from "../../models/spu-paging";
import {CouponCenterType} from "../../core/enum";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        themeA: null,
        themeE: null,
        bannerB: null,
        grid: [],
        activityD: null,
        spuPaging: null,
        loadingType: 'loading'
    },

    async onLoad(options) {
        this.initAllData()
        this.initBottomSpuList()
    },

    async initBottomSpuList() {
        const paging = SpuPaging.getLatestPaging()
        this.data.spuPaging = paging
        const data = await paging.getMoreData()
        if (!data) {
            return
        }

        wx.lin.renderWaterFlow(data.items)


    },

    async initAllData() {
        const theme = new Theme()
        await theme.getThemes()

        const themeA = theme.getHomeLocationA()
        const themeE = theme.getHomeLocationE()
        // console.log('themeA', themeA)
        console.log('themeE', themeE)
        let themeESpu = []

        if (themeE!=null && themeE.online) {
            const data = await Theme.getHomeLocationESpu()
            if (data) {
                // console.log('data.spu_list', data, data.spu_list)
                // themeESpu = data.spu_list.slice(0, 8)
                themeESpu = data.spuList.slice(0, 8)
                themeESpu.map(o => {
                    o.w = 100
                    o.h = 100
                })
                // console.log('themeESpu', themeESpu)
            }
        }

        const themeF = theme.getHomeLocationF()
        // console.log('->themeF', themeF)

        const bannerB = await Banner.getHomeLocationB()
        const grid = await Category.getHomeLocationC()

        const activityD = await Activity.getHomeLocationD()
        console.log('->activityD', activityD)

        const bannerG = await Banner.getHomeLocationG()
        console.log('->bannerG', bannerG)

        const themeH = theme.getHomeLocationH()
        // console.log('->themeH', themeH)

        this.setData({
            themeA,
            bannerB,
            grid,
            activityD,
            themeE,
            themeESpu,
            themeF,
            bannerG,
            themeH
        })
    },

    onGoToCoupons(event) {
        const name = event.currentTarget.dataset.aname
        wx.navigateTo({
            url: `/pages/coupon/coupon?name=${name}&type=${CouponCenterType.ACTIVITY}`
        })
    },

    onGoToTheme(event) {
        const tName = event.currentTarget.dataset.tname
        wx.navigateTo({
            url: `/pages/theme/theme?tname=${tName}`
        })
    },

    onGoToBanner(event) {
        const keyword = event.currentTarget.dataset.keyword
        const type = event.currentTarget.dataset.type
        Banner.gotoTarget(type, keyword)
    },

    onReachBottom: async function () {
        // console.log('home onReachBottom...', this.data.spuPaging)
        if (this.data.spuPaging.moreData) {
            const data = await this.data.spuPaging.getMoreData()
            if (!data) {
                return
            }
            wx.lin.renderWaterFlow(data.items)
            if (!data.moreData) {
                this.setData({
                    loadingType: 'end'
                })
            }
        } else {
            this.setData({
                loadingType: 'end'
            })
        }
    },

    onPullDownRefresh: function () {
        console.log('home onPullDownRefresh...')
    },

    onShareAppMessage: function () {
        console.log('home onShareAppMessage...')
    },

    onGoToUs(event) {
        console.log('->onGoToUs', event)
        wx.navigateTo({
            url: `/pages/about/about`
        })
    },

    onGoToTiaokuan(event) {
        console.log('->onGoToTiaokuan', event)
        wx.navigateTo({
            url: `/pages/about-course/about-course`
        })
    },
})


